package com.hup.utils.swing;

import javax.swing.*;
import java.awt.*;

/**
 * @author hugan
 * @date 2020/4/14
 */
public class GraphicsUtil {

    /**
     * 开启文字和图像反锯齿
     * @param g 一般在 {@link JComponent#paint(Graphics)}中使用
     */
    public static Graphics2D antiAlias(Graphics2D g) {
        return antiAlias(g, true, true);
    }

    /**
     * 开启文字和图像反锯齿
     * @param g 一般在 {@link JComponent#paint(Graphics)}中使用
     * @apiNote 开启后,直接draw[字符串,圆]时,能看出来像素边缘模糊了;
     * 但g.drawImage好像不会?
     */
    public static Graphics2D antiAlias(Graphics2D g, boolean text, boolean img) {
        if (text) g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        if (img) g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        return g;
    }

}
